package orm.select;

import co.streamx.fluent.extree.expression.LambdaExpression;
import orm.analysis.ToSqlVisitor;
import orm.delegates.Action1;
import orm.delegates.SinglePredicate;

import java.util.ArrayList;
import java.util.List;

public class Select<T> {
    private List<Action1<Where<T>>> wheres = new ArrayList<>();
    private List<OrderByObject<T>> orderByCollect = new ArrayList<>();

    public Select<T> where(Action1<Where<T>> where) {
        wheres.add(where);
        return this;
    }

    public Select<T> orderBy(SinglePredicate<T> lambda) {
        orderByCollect.add(new OrderByObject<T>(1, lambda));
        return this;
    }

    public String toSql() {
        StringBuilder sb = new StringBuilder();
        for (Action1<Where<T>> where : wheres) {
            Where w = new Where();
            where.apply(w);
            var r = w.end();
            if (sb.length() != 0) {
                sb.append(" AND (" + r + ") ");
            } else {
                sb.append("(" + r + ")");
            }
        }

        for (OrderByObject<T> orderBy : orderByCollect) {
            LambdaExpression<SinglePredicate<T>> lambdaExpression = LambdaExpression.parse(orderBy.lambda);
            ToSqlVisitor toSqlVisitor = new ToSqlVisitor();
            lambdaExpression.accept(toSqlVisitor);
            System.out.println("====sql====" + toSqlVisitor.GetWhere());
        }
        return sb.toString();
    }
}

